Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java inheritance

    Inheritance is a critical component of OOP (Object-Oriented Programming). It is the mechanism in Java that allows one class to inherit the characteristics (fields and methods) of another class. In Java, inheritance refers to the process of developing new classes from existing ones. A class can use the fields and methods of another class from which it has inherited.. You can also add additional fields and methods to your current class.

Table Of Content
  • What is inheritance in Java
  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance





Usage of inheritance in Java

  • Code Reusability: All subclasses share the code written in the Superclass. The parent class code can be used directly by child classes.
  • Method Overriding:Only inheritance allows for Method Overriding. It is one method for achieving Run Time Polymorphism in Java.
  • Abstraction:Inheritance allows us to achieve the concept of abstraction, where we are not required to provide all the details. Abstraction only makes the user aware of the functionality.

Important Terms used in Inheritance

  • Class: A class is a collection of objects that have similar traits, behaviours, and attributes. Class does not exist in the real world. It simply serves as a model, blueprint, or prototype from which objects can be made.
  • Super Class or Parent Class:A superclass is a class from which features are inherited
  • Sub Class or Child Class:A subclass is a class that inherits from another class (or a derived class, extended class, or child class). In addition to the fields and methods of the superclass, the subclass may also add additional fields and methods.
  • Reusability:The idea of "reusability" is supported by inheritance; for example, if we want to create a new class but an existing class already contains some of the code we need, we can derive our new class from the existing class. We are utilising the fields and methods of the pre-existing class by doing this.

Fact about Java inheritance

  • Default superclass: Every class, with the exception of the Object class, has a single direct superclass (single inheritance). Every class is implicitly a subclass of the Object class in the absence of any other explicit superclass.
  • Only one Superclass:Any number of subclasses can exist under a superclass. But there can only be one superclass per subclass. This is due to Java's lack of support for multiple class inheritances. Java does support multiple inheritances, but only with interfaces.
  • Inherite Constructors:Fields, methods, and nested classes are all members that a subclass inherits from its superclass. Subclasses cannot inherit constructors because they are not members; however, they may call the constructor of the superclass.
  • Inheritance with Private member :The private members of a parent class are not inherited by a subclass. However, the subclass may also use the public or protected methods that the superclass provides (such as getters and setters) to access its private fields.

The syntax of Java Inheritance

  • Java uses the extends keyword to describe inheritance. When you use the extends keyword, it shows that you are a derived class. In other words, "extends" means that the functionality has been increased.
  • Syntax

     class  Subclass-name extends   Superclass-name   
         statements;  
      } 


Types of inheritance in java

  • In Java, there are three different types of inheritance that can be used:
    • single inheritance
    • multilevel inheritance
    • hierarchical inheritance
  • Multiple and hybrid inheritance are only supported through interfaces in Java programming.

1.Single Inheritance


Subclasses inherit the characteristics of a single superclass under single inheritance.


Java program with single inheritance
 // Java program with single inheritance  
class Shape{  
    void Draw(){
        System.out.println("Drawing...");
    }  
 }  
  class Circle extends Shape{ 
      void CircleDraw(){
          System.out.println("Circle Drawing....");
      }
 }  
  class Inheritance{  
      public static void main(String args[]){  
          Circle Circle=new Circle(); 
          Circle.Draw(); 
          Circle.CircleDraw();  	
      }
 }
  




Output:

Drawing... 
 
Circle Drawing....

2.Multilevel Inheritance


A derived class will inherit a base class in multilevel inheritance, and in addition, the derived class will serve as the base class for other classes.


Java program with Multilevel inheritance
 // Java program with Multilevel inheritance 
class Shape{  
    void Draw(){
        System.out.println("Drawing...");
    }  
  }  
  class Circle extends Shape{ 
      void CircleDraw(){
          System.out.println("Circle Drawing....");
      }
  }  
  class SemiCircle extends Circle{ 
      void SemiCircleDraw(){
          System.out.println("Semicircle Drawing....");
      }
  }
  class Inheritance{  
      public static void main(String args[]){  
          SemiCircle semiCircle=new SemiCircle(); 
          semiCircle.Draw(); 
          semiCircle.CircleDraw();
          semiCircle.SemiCircleDraw();
      }
  }
  



Output:

Drawing... 
 
Circle Drawing....
 
Semicircle Drawing....

3.Hierarchical Inheritance


One class acts as the superclass (base class) for multiple subclasses in hierarchical inheritance.


Java program using hierarchical Inheritance
 // Java program  using hierarchical Inheritance 
class Shape{  
    void Draw(){
        System.out.println("Drawing...");
    }  
  }  
  class Circle extends Shape{ 
      void CircleDraw(){
          System.out.println("Circle Drawing....");
      }
  }  
  class SemiCircle extends Shape{ 
      void SemiCircleDraw(){
          System.out.println("Semicircle Drawing....");
      }
  }
  class Inheritance{  
      public static void main(String args[]){  
          SemiCircle semiCircle=new SemiCircle(); 
          semiCircle.Draw(); 
  //		semiCircle.CircleDraw();    //Compile Time Error
          semiCircle.SemiCircleDraw();
      }
  }
  



Output:

Drawing... 
 
Semicircle Drawing....

4.Multiple Inheritance in java using Interfaces


A class can have more than one superclass and receive features from all of its parent classes when there are multiple inheritances. Please be aware that multiple inheritances with classes are not supported by Java. Only by using interfaces in Java is multiple inheritance possible.


Java program Multiple Inheritance using Interfaces
 // Java program Multiple Inheritance using Interfaces 
  import java.util.*;

    interface  Shape{  
        public void Draw(); 
    }  
    interface  Circle extends Shape{ 
        public void CircleDraw();
    }  
    interface  SemiCircle extends Circle, Shape{ 
        public void SemiCircleDraw();
    }
    class Test implements SemiCircle {
    
        @Override
        public void CircleDraw() {
            System.out.println("Circle Drawing....");		
        }
        @Override
        public void Draw() {
            System.out.println("Drawing....");	
        }
        @Override
        public void SemiCircleDraw() {
            System.out.println("Semi Circle Drawing....");	
        }
    }
    
    class Inheritance{  
        public static void main(String args[]){  
            Test test=new Test(); 
            test.Draw(); 
            test.CircleDraw();   
            test.SemiCircleDraw();
        }
    }
    



Output:

Drawing... 
 
Circle Drawing....
 
Semicircle Drawing....

5.Hybrid Inheritance using Interfaces


It combines two or more of the already defined inheritance patterns. Hybrid inheritance is not possible with classes because Java does not support multiple inheritances. Only by using interfaces in Java is hybrid inheritance possible.


Java Hybrid Inheritance Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.